/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Servidor.Controlador;

import Servidor.Modelo.Property;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Jorge Perez, Isabel Echeveste
 * 
 */
public class MainController {

    private static int port = Property.getInstance().getPort();

    public static void main(String[] args) {


        ServerSocket servSock = null;
        try {
            servSock = new ServerSocket(port);
        } catch (IOException ex) {
            Logger.getLogger(MainController.class.getName()).log(Level.SEVERE, null, ex);
        }
        while (true) {
            try {
                Socket incoming = servSock.accept();
                BufferedReader read = new BufferedReader(new InputStreamReader(incoming.getInputStream()));
                PrintWriter write = new PrintWriter(incoming.getOutputStream(), true);

                String order = null;

                try {
                    order = read.readLine();

                } catch (IOException ex) {
                    System.out.println("ERROR con la IO");
                }
                if (order.isEmpty()) {
                    read.close();
                    incoming.close();
                } else {
                    OcaTask t = null;
                    if (order.equalsIgnoreCase(ConnectionConstraints.LOGIN)) {
                        t = new LoginTask(incoming, read, write);
                    } else if (order.equalsIgnoreCase(ConnectionConstraints.GAME_NEW)) {
                        t = new NewGameTask(incoming, read, write);
                    } else if (order.equalsIgnoreCase(ConnectionConstraints.GAME_START)) {
                        t = new StartGameTask(incoming, read, write);
                    } else if (order.equalsIgnoreCase(ConnectionConstraints.GAME_THROW_DICE)) {
                        t = new ThrowDiceTask(incoming, read, write);
                    } else if (order.equalsIgnoreCase(ConnectionConstraints.GAME_EXIT)) {
                        t = new LeaveGameTask(incoming, read, write);
                    } else if (order.equalsIgnoreCase(ConnectionConstraints.GAME_JOIN)) {
                        t = new JoinGameTask(incoming, read, write);
                    } else if (order.equalsIgnoreCase(ConnectionConstraints.GAME_FINISH)) {
                       t = new FinishGame(incoming, read,write); 
                    } else if (order.equalsIgnoreCase(ConnectionConstraints.USER_CREATE)) {
                        t = new AddUserTask(incoming, read, write);
                    } else if (order.equalsIgnoreCase(ConnectionConstraints.USER_DELETE)) {
                        t = new DeleteUserTask(incoming, read, write);
                    } else if (order.equalsIgnoreCase(ConnectionConstraints.USER_LIST)) {
                        t = new ListUsersTask(incoming, read, write);
                    } else if (order.equalsIgnoreCase(ConnectionConstraints.USER_QUERY_BY_CODE)) {
                        t = new QueryByCodeUserTask(incoming, read, write);
                    } else if (order.equalsIgnoreCase(ConnectionConstraints.USER_UPDATE)) {
                        t = new ModifyUserTask(incoming, read, write);
                    } else if (order.equalsIgnoreCase(ConnectionConstraints.GAMES_LIST_ACTIVE)) {
                        t = new ListActiveGamesTask(incoming, read, write);
                    } else {
                        t = new TrashTask(incoming, read, write);
                    } 
                    t.executeTask();
                }

            } catch (UnknownHostException ex) {
                Logger.getLogger(MainController.class.getName()).log(Level.SEVERE, null, ex);
            } catch (IOException ex) {
                Logger.getLogger(MainController.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }
}
